package com.yz.jdd.widget;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.yz.jdd.R;
import com.yz.jdd.base.event.Events;
import com.yz.jdd.base.event.RxBus;
import com.yz.jdd.base.utils.BaseConfigs;
import com.yz.jdd.base.utils.LogUtils;
import com.yz.jdd.base.utils.PayConfig;
import com.yz.jdd.base.utils.Tools;

import rx.Subscriber;
import rx.Subscription;
import rx.functions.Action0;

/**
 * Created by bin on 2017/3/24.
 */

public class CountdownLoadingDialog extends Dialog {
    Subscription subscription;
    private Button loadingButton;
    private LinearLayout buttonView;
    private TextView loading_count;
    private TextView loading_msg;


    /**
     * 一分钟倒计时loading
     *
     * @param context
     * @param orderType
     */
    public CountdownLoadingDialog(final Context context, final String orderType) {
        super(context, R.style.Cate_Dialog);
        setContentView(R.layout.loading_dialog_pay);
        findViewById(R.id.pay_loading_msg).setVisibility(View.VISIBLE);
        loading_count = (TextView) findViewById(R.id.pay_loading_count);
        loading_msg = (TextView) findViewById(R.id.pay_loading_msg);
        loading_msg.setText("");

        buttonView = (LinearLayout) findViewById(R.id.pay_layout_bottom);
        loadingButton = (Button) findViewById(R.id.pay_btn_bottom);
        loadingButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //撤销收款操作
                RxBus.getInstance().send(Events.EVENT_CANCEL_PAY, Events.EVENT_CANCEL_PAY);
                dismiss();
            }
        });
        subscription = Tools.countdown(BaseConfigs.COUNT_COUTDOWN).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                LogUtils.d(" ====== start countdown");
            }
        }).subscribe(new Subscriber<Integer>() {
            @Override
            public void onCompleted() {
                LogUtils.d(" ====== countdown onCompleted");
                RxBus.getInstance().send(Events.PAY_WAITING_COUNTDOWN_FINISH, Events.PAY_WAITING_COUNTDOWN_FINISH);
//                dismiss();
                loading_count.setVisibility(View.GONE);
                setLoadingMsg("您已等待1分钟，该订单收款结果未知，请关闭订单后重新收款");
            }

            @Override
            public void onError(Throwable e) {
                dismiss();
            }

            @Override
            public void onNext(Integer integer) {
                LogUtils.d("countdown  ====== " + integer);
                loading_count.setText(integer + "秒");
                if (BaseConfigs.COUTN_SHOW_CANCEL == integer && !PayConfig.TYPE_ORDER_DISH.equals(orderType)) {
                    buttonView.setVisibility(View.VISIBLE);
                }
            }
        });
        initLayoutWindow();
    }

    private void initLayoutWindow() {
        WindowManager.LayoutParams p = getWindow().getAttributes();
        p.gravity = Gravity.CENTER;
        p.height = WindowManager.LayoutParams.WRAP_CONTENT;//(int) (d.getWidth() * 0.5);   //高度设置为屏幕的0.4
        p.width = WindowManager.LayoutParams.WRAP_CONTENT;//(int) (d.getWidth() * 0.85);    //宽度设置为屏幕的0.7
        getWindow().setAttributes(p);
    }

    /**
     * 关闭倒计时
     */
    public void shutDownCount() {
        if (subscription != null) {
            subscription.unsubscribe();
        }
    }

    public void setLoadingMsg(String msg) {
        if (!TextUtils.isEmpty(msg)) {
            loading_msg.setText(msg);
        }
    }

    public void showCancelButton(String msg) {
        buttonView.setVisibility(View.VISIBLE);
        loadingButton.setText(msg);

    }

    @Override
    public void dismiss() {
        if (subscription != null) {
            subscription.unsubscribe();
        }
        super.dismiss();

    }
}
